﻿using Microsoft.ManagementConsole.SnapIns.Integration;
using PlanckTime.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Microsoft.ManagementConsole.SnapIns.Controls
{
    public partial class LoginForm : Form
    {
        public bool IsValidLogin { get; set; }

        public LoginForm()
        {
            InitializeComponent();

            this.Text = Environment.UserName;
            this.AcceptButton = this.button1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Text = "Waiting...";

            if (!string.IsNullOrEmpty(txtPassword.Text))
            {
                User user = new User()
                {
                    Name = Environment.UserDomainName + "\\" + Environment.UserName,
                    Pin = txtPassword.Text
                };

                var result=PlanckTimeServices.Instance.Authenticate(user);

                if (result != null)
                {
                    IsValidLogin = true;
                    this.Close();
                }
                else
                {
                    IsValidLogin = false;

                    this.Text = Environment.UserName;

                    MessageBox.Show("Invalid Pin");
                }
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
